
<style lang="less">
  .page__wrapper {
    width: 100%;
    height: 100%;
  }
</style>

<template>
  <div class="page__wrapper">
    <simple-table :table-column="tableColumn" :table-data="tableData" :table-page-info="tablePageInfo" :table-data-count="tableDataTotalCount" @page-info-change="handlePageInfoChange">
      <template slot="searchSection">
        <simple-form v-model="searchModel" :form-field="searchFormField">
          <template slot="tableOperation">
            <el-button @click="handleDataSearch">{{$t('operation.search')}}</el-button>
            <!-- <el-button v-if="actionList.indexOf('add') >= 0" type="primary" @click="handleAddClick">{{$t('operation.create')}}</el-button> -->
            <el-button type="primary" @click="handleExportTemplate">模版</el-button>
            <el-button type="danger" @click="handleChangeLockStatus">加解锁</el-button>
            <el-button type="primary" @click="handleExport">导出</el-button>
            <aries-import v-if="actionList.indexOf('import') >= 0" tableName="Biz_Diversified_Business"></aries-import>
          </template>
        </simple-form>
      </template>
      <template slot-scope="scope" slot="plannedcompletiontime">
        {{ typeof(scope.row.plannedcompletiontime) === 'string' ? scope.row.plannedcompletiontime.substr(0, 10) : '' }}
      </template>
      <template slot-scope="scope" slot="isbelongedtounlisted">
        {{ $getDictLabel({type: '是否', value: scope.row.isbelongedtounlisted}) }}
      </template>
      <template slot-scope="scope" slot="uploadquarter">
        {{ $getDictLabel({type: '季度', value: scope.row.uploadquarter}) }}
      </template>
      <template slot-scope="scope" slot="propertytype">
        {{$getDictLabel({type: '产权性质_多种经营', value : scope.row.propertytype})}}
      </template>
      <template slot-scope="scope" slot="businessstatus">
        {{$getDictLabel({type: '经营状态_多种经营', value : scope.row.businessstatus})}}
      </template>
      <template slot-scope="scope" slot="businesstype">
        {{$getDictLabel({type: '业务类型', value : scope.row.businesstype})}}
      </template>
      <template slot-scope="scope" slot="reformmethod">
        {{$getDictLabel({type: '改革方式_多种经营', value : scope.row.reformmethod})}}
      </template>
      <template slot-scope="scope" slot="companyname">
        {{ scope.row.companyname | orgFilter(orgList)}}
      </template>
      <template slot="operation" slot-scope="scope">
        <el-button v-if="actionList.indexOf('view') >= 0" circle icon="el-icon-search" :title="$t('operation.view')" @click="handleViewClick(scope.row)"></el-button>
        <el-button v-if="actionList.indexOf('edit') >= 0" circle icon="el-icon-edit" :title="$t('operation.edit')" @click="handleEditClick(scope.row)"></el-button>
        <el-button v-if="actionList.indexOf('del') >= 0" @click="deleteRecord(scope.row)" circle icon="el-icon-delete" :title="$t('operation.delete')"></el-button>
      </template>
    </simple-table>
    <detail-dialog :show-flag.sync="isShowDetailDialog" :detail-data="rowData" :view-flag="isViewFlag" :ext-data="{sys_mid, orgList}"></detail-dialog>
  </div>
</template>
<script>
import Axios from 'axios'
/** 混入分页信息及数据总数信息 */
import pageMixins from '@/components/base/simple-table/page-mixins'
import detailDialog from './detail'
const BASEURL = {
  list: `/Web/BizController/ajax.html?sys_method=GetListBySql&sys_objName=Biz_Diversified_Business&sys_tableName=Biz_Diversified_Business`,
  delete: '/Web/SysAdmin/ajax.html?sys_method=Delete&sys_objName=Biz_Diversified_Business&sys_tableName=Biz_Diversified_Business',
  getCombobox: '/Web/SysAdmin/ajax.html?sys_method=GetCombobox',
  getInitConfig: '/Web/SysAdmin/ajax.html?sys_method=GetInitConfig'
}
export default {
  name: 'balance_diversification',
  mixins: [pageMixins],
  components: { detailDialog },
  data () {
    return {
      searchModel: {
        uploadyear: this.$moment().format('YYYY'),
        uploadquarter: String(this.$moment().quarter())
      },
      /** 表格数据 */
      tableData: [],
      rowData: {},
      /** 是否显示详情页 dialog */
      isShowDetailDialog: false,
      /** 是否为查看数据 */
      isViewFlag: false,
      actionList: [],
      sys_mid: '',

      orgList: []
    }
  },
  filters: {
    orgFilter (value, orgList) {
      if (!value) return ''
      let valueArr = value.split(',')
      let resultStr = []
      valueArr.forEach(valueItem => {
        let cacheOrg = orgList.filter(orgItem => orgItem.value === valueItem)
        if (cacheOrg.length > 0) resultStr.push(cacheOrg[0].text)
      })
      return resultStr.join()
    }
  },
  computed: {
    searchFormField () {
      return [
        { prop: 'companyname', label: '企业名称', type: 'Select', col: { span: 8 }, component: { clearable: true, optionList: this.orgList } },
        { prop: 'uploadyear', label: '年份', type: 'Datepicker', col: { span: 8 }, component: { clearable: false, type: 'year', valueFormat: 'yyyy' } },
        { prop: 'uploadquarter', label: '季度', type: 'Select', col: { span: 8 }, component: { clearable: false, optionList: this.$getDictList('季度') } },
        { slotName: 'tableOperation', col: { span: 24, style: { textAlign: 'right' } }, labelWidth: '0' }
      ]
    },
    tableColumn () {
      return [
        { prop: 'companyname', label: '企业名称', slotName: 'companyname', width: 160, fixed: 'left' },
        { prop: 'uploadyear', label: '年份', width: 50, fixed: 'left' },
        { prop: 'uploadquarter', label: '季度', slotName: 'uploadquarter', width: 70, fixed: 'left' },

        // 基本情况
        {
          label: '基本情况',
          children: [
            { prop: 'managementlevel', label: '管理层级' },
            { prop: 'propertytype', label: '产权性质', slotName: 'propertytype' },
            { prop: 'equitystructure', label: '股权结构' },
            { prop: 'businessstatus', label: '经营状态', slotName: 'businessstatus' },
            { prop: 'plannedcompletiontime', label: '计划完成时间', width: 100, slotName: 'plannedcompletiontime' },
            { prop: 'isbelongedtounlisted', label: '是否属于未上市托管企业', width: 100, slotName: 'isbelongedtounlisted' },
            { prop: 'businesstype', label: '业务类型', slotName: 'businesstype', width: 100 }]
        },
        // 改革推进情况
        {
          label: '改革推进情况',
          children: [
            { prop: 'reformmethod', label: '改革方式', slotName: 'reformmethod' },
            { prop: 'reformmeasuresprogress', label: '改革措施与进展', width: 120 },
            { prop: 'existingproblem', label: '存在问题' },
            { prop: 'nextworkarrangement', label: '下一步工作安排', width: 120 }]
        },

        // 资产负债情况
        {
          label: '资产负债情况',
          children: [
            {
              label: '期末数',
              children: [
                { prop: 'totalassets', label: '资产总额' },
                { prop: 'currencyfund', label: '其中：货币资金', width: 120 },
                { prop: 'stocknetreceivables', label: '存货和应收账款净额', width: 150 },
                { prop: 'netvalueoffixedassets', label: '固定资产净额', width: 120 },
                { prop: 'totalliabilities', label: '负债总额' },
                { prop: 'debtwithinterest', label: '其中：带息负债金额', width: 150 },
                { prop: 'assetsliabilities', label: '资产负债率', width: 100 }]
            }]
        },

        // 主要经营情况
        {
          label: '主要经营情况',
          children: [
            {
              label: '本年累计',
              children: [
                {
                  label: '营业收入',
                  children: [
                    { prop: 'totalincome', label: '收入总额' },
                    { prop: 'unitsponsoredincome', label: '其中：来自主办单位的收入', width: 180 }
                  ]
                },
                {
                  label: '成本费用',
                  children: [
                    { prop: 'totalcost', label: '费用总额' },
                    { prop: 'laborcost', label: '其中：人工成本', width: 120 },
                    { prop: 'interestexpense', label: '利息支出' },
                    { prop: 'depreciationdepletion', label: '折旧折耗' },
                    { prop: 'assetimpairmentloss', label: '资产减值损失', width: 120 }
                  ]
                },
                { label: '利润总额', prop: 'totalprofit' },
                { prop: 'investmentincome', label: '投资收益' },
                { prop: 'dividend', label: '分红' },
                { prop: 'profitlossreason', label: '盈亏原因分析', width: 120 }
              ]
            }
          ]
        },

        // 在职人员情况
        {
          label: '在职人员情况',
          children: [
            {
              label: '本年累计人员变动减员',
              children: [{
                label: '减员',
                children: [
                  { prop: 'subtotalreduction', label: '减员小计' },
                  { prop: 'naturalreduction', label: '自然减员' },
                  { prop: 'agreementcancellation', label: '协议解除劳动合同', width: 150 },
                  { prop: 'earlyretirement', label: '提前退休' },
                  { prop: 'otherreduction', label: '其他' }]
              }, {
                label: '增员',
                children: [
                  { prop: 'subtotalincrease', label: '增员小计' },
                  { prop: 'contractemployee', label: '合同化用工', width: 100 },
                  { prop: 'marketemployee', label: '市场化用工', width: 100 },
                  { prop: 'laboremployee', label: '劳务用工' },
                  { prop: 'otherincrease', label: '其他' }]
              }]
            }, {
              label: '在职人数',
              children: [
                { prop: 'contractemployeecurrent', label: '合同化员工', width: 100 },
                { prop: 'marketemployeecurrent', label: '市场化用工', width: 100 },
                { prop: 'laboremployeecurrent', label: '劳务用工' }]
            }, {
              prop: 'currentemployeefinally', label: '期末在职人数', width: 100
            }]
        },
        { label: this.$t('field.operation'), slotName: 'operation', width: 100, fixed: 'right', align: 'center' }
      ]
    }
  },
  /**
   * 注入到子组件
   */
  provide () {
    return { handleDataSearch: this.handleDataSearch }
  },
  methods: {
    handleDataSearch () {
      let params = Object.assign({}, this.tablePageInfo, { sys_mid: this.sys_mid, UserID: this.$store.state.app.userInfo.userid }, { sys_search: this.searchModel })

      let loadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
      this.$axios
        .post(BASEURL.list, params)
        .then(resp => {
          this.tableData = resp.rows
          this.tableDataTotalCount = resp.total
        })
        .finally(() => {
          loadingFlag.close()
        })
    },

    handleInitData () {
      let configLoadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })
      this.$axios
        .post(BASEURL.getInitConfig, {})
        .then(resp => {
          // 设置 actionList及sys_mid
          this.actionList = resp.actionKeys.split(',')

          this.sys_mid = resp.mid
          /** 设置列表及涉及主数据请求参数 */
          // let params = Object.assign({}, this.tablePageInfo, { sys_mid: resp.mid }, { sys_search: this.searchModel })
          let params = Object.assign({}, this.tablePageInfo, { sys_mid: resp.mid, UserID: this.$store.state.app.userInfo.userid }, { sys_search: this.searchModel })
          let comboParams = { sys_json: JSON.stringify([{ ObjName: 'C_SYS_Org' }]) }
          let loadingFlag = this.$loading({ fullscreen: false, target: this.$el, text: this.$t('tip.systemLoading') })

          Axios.all([
            this.$axios.post(BASEURL.list, params),
            this.$axios.post(BASEURL.getCombobox, comboParams)
          ])
            .then(Axios.spread((pageData, comboData) => {
              this.tableData = pageData.rows
              // this.tablePageInfo = { pageNo: respData.pageNo, pageSize: respData.pageSize }
              this.tableDataTotalCount = pageData.total

              this.orgList = comboData.C_SYS_Org
            }))
            .finally(() => {
              loadingFlag.close()
            })
        })
        .finally(() => {
          configLoadingFlag.close()
        })
    },

    handleAddClick () {
      this.rowData = {}
      this.isViewFlag = false
      this.isShowDetailDialog = true
    },
    handleViewClick (row) {
      this.rowData = this.$_.cloneDeep(row)
      this.isViewFlag = true
      this.isShowDetailDialog = true
    },
    handleEditClick (row) {
      this.rowData = this.$_.cloneDeep(row)
      this.isViewFlag = false
      this.isShowDetailDialog = true
    },
    handleExportTemplate () {
      this.$utils.fileExportAs('/Web/SysAdmin/ajax.html', {
        sys_objName: 'Biz_Diversified_Business',
        sys_tableName: 'Biz_Diversified_Business',
        sys_method: 'ExcelTemplate',
        sys_mid: this.sys_mid
      }, '多种经营业务信息收集表.xls')
    },
    handleExport () {
      this.$utils.fileExportAs('/Web/BizController/ajax.html', {
        sys_objName: 'Export_Diversified_Business',
        sys_tableName: 'Export_Diversified_Business',
        sys_mid: this.sys_mid,
        sys_method: 'QueryExport',
        sys_search: this.searchModel,
        // 业务部分······
        dataStartRowIndex: 6, // 导出的excel对应的数据起始行
        dataStartCollumIndex: 0, // 导出的excel对应的数据起始列
        specialInfo: `${this.searchModel.uploadyear}年${this.$getDictLabel({ type: '季度', value: this.searchModel.uploadquarter })}        单位：万元，人`,
        specialStartRowIndex: 1, // 定位表头坐标
        specialStartCollumIndex: 0 // 定位表头坐标
      }, '多种经营业务信息收集表.xls')
    },

    /** 更改数据锁定状态 */
    handleChangeLockStatus () {

    },

    deleteRecord (row) {
      this.$msgbox({
        title: this.$t('tip.warning'),
        message: this.$t('tip.confirmDelete'),
        type: 'warning',
        showCancelButton: true,
        closeOnClickModal: false,
        closeOnPressEscape: false,
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            this.$axios
              .post(BASEURL.delete, { id: row.id, idField: 'id', sys_mid: this.sys_mid })
              .then(resp => {
                this.$message({ type: 'success', message: this.$t('tip.deleteSuccess') })
                done()
              })
              .finally(() => {
                instance.confirmButtonLoading = false
              })
          } else done()
        }
      })
        .then(() => {
          /** 如果本页面中只有一条数据 */
          /** 手动触发 simple-table 组件中的 watch，从而调用 mixins 中的请求数据方法 */
          if (this.tableData.length === 1) {
            this.tablePageInfo.page--
          } else {
            this.handleDataSearch()
          }
        })
    }
  },
  mounted () {
    this.handleInitData()
  }
}
</script>
